﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data.OleDb;
using System.Windows.Forms;

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.DataSourcesFile;

namespace shudiangis.Common
{
    public class UserDatabaseOperation
    {
        #region 操作ACCESS库
        /// <summary>
        /// 根据数据文件路径获取WORKSPACE
        /// </summary>
        /// <param name="strAccessPath">access文件路径[For example, strAccessPath = "C:\\myData\\mypGDB.mdb"]</param>
        /// <returns></returns>
        public static IWorkspace GetWorkspaceBySetPerproty(string strAccessPath)
        {
            IPropertySet propertySet = new PropertySetClass();
            propertySet.SetProperty("DATABASE", strAccessPath);
            IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();

            return workspaceFactory.Open(propertySet, 0);
        }

        /// <summary>
        /// 根据数据文件路径获取WORKSPACE
        /// </summary>
        /// <param name="strAccessPath">access文件路径[For example, strAccessPath = "C:\\myData\\mypGDB.mdb"]</param>
        /// <returns></returns>
        public static IWorkspace GetWorkspaceBySetFile(string strAccessPath)
        {
            IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
            return workspaceFactory.OpenFromFile(strAccessPath, 0);
        }

        /// <summary>
        /// 获取影象数据集合
        /// </summary>
        /// <param name="workSpace">工作空间对象</param>
        /// <param name="datasetName">数据集名</param>
        /// <returns></returns>
        public static IRasterDataset OpenFileRasterDataset(IWorkspace workSpace, string datasetName)
        {
            IRasterWorkspace rasterWorkspace = workSpace as IRasterWorkspace;
            IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(datasetName);
            return rasterDataset;
        }


        /// <summary>
        /// 获取图例
        /// </summary>
        /// <param name="workSpace">工作空间对象</param>
        /// <param name="strTableName">数据表名</param>
        /// <returns></returns>
        public static IFeatureClass GetFeature(IWorkspace workSpace, string strTableName)
        {
            IFeatureWorkspace featureWorkspace = workSpace as IFeatureWorkspace;
            IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(strTableName);

            return featureClass;
        }

        /// <summary>
        /// 获取表
        /// </summary>
        /// <param name="workSpace">工作空间对象</param>
        /// <param name="strTableName">数据表名</param>
        /// <returns></returns>
        public static ITable GetTable(IWorkspace workSpace, string strTableName)
        {
            IFeatureWorkspace featureWorkspace = workSpace as IFeatureWorkspace;
            ITable table = featureWorkspace.OpenTable(strTableName);

            return table;
        }
        
        #endregion
 
        /// <summary>
        /// 连接SHARP文件(location CANNOT be relative path ".\data\usa" it must be explicit pathname)
        /// </summary>
        /// <param name="Location"></param>
        /// <returns></returns>
        public IWorkspace openShapfileWorkspace(string Location)
        {
            IWorkspace ws = null;
            IWorkspaceFactory wsf = new ShapefileWorkspaceFactoryClass();
            ws = wsf.OpenFromFile(Location, 0);
            return ws;
        }

        /// <summary>
        /// 连接企业库(create and open the sde workspace based on the provided information)
        /// </summary>
        /// <param name="Server"></param>
        /// <param name="Service"></param>
        /// <param name="Instance"></param>
        /// <param name="User"></param>
        /// <param name="Password"></param>
        /// <param name="Database"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        public static IFeatureWorkspace openSDEWorkspace(string Server, string Service, string Instance, string User,
                                           string Password, string Database, string version)
        {          

            //Workspace           
            IWorkspaceFactory pWorkspaceFactory;
            pWorkspaceFactory = new SdeWorkspaceFactoryClass();

            //PropertySet
            IPropertySet pPropSet;
            pPropSet = new PropertySetClass();

            pPropSet.SetProperty("SERVER", Server);
            pPropSet.SetProperty("SERVICE", Service);
            pPropSet.SetProperty("INSTANCE", Instance);
            //pPropSet.SetProperty("DATABASE", Database);
            pPropSet.SetProperty("USER", User);
            pPropSet.SetProperty("PASSWORD", Password);
            pPropSet.SetProperty("VERSION", version);

            //IFeatureWorkspace对象
            IFeatureWorkspace pSdeWorkspace=null;           
            
            try
            {
                //连接
                pSdeWorkspace = (IFeatureWorkspace)pWorkspaceFactory.Open(pPropSet, 0);
            }
            catch (Exception ex)
            {
                MessageBox.Show("SDE数据库不能连接！" + ex.Message);
            }

            return pSdeWorkspace;
        }

        /// <summary>
        /// OLEDB的连接
        /// </summary>
        /// <returns></returns>
        public static OleDbConnection GetSdeConnectionByOledb()
        {
            OleDbConnection sdeConn = new OleDbConnection();
            String oledbConnectionString;
          
            // Set the Connection String
            oledbConnectionString = "Provider=ESRI.GeoDB.OleDB.1;Location=balrog;Data Source=sde;User Id=vtest;Password=go;Extended Properties=workspacetype=esriDataSourcesGDB.SdeWorkspaceFactory.1;Geometry=WKB;Instance=5150;Version=SDE.DEFAULT";
            sdeConn.ConnectionString = oledbConnectionString;

            // Open the Connection
            sdeConn.Open();

            return sdeConn;
        }

    }
}
